// pages/MaintanHistorySearch/MaintanHistorySearch.js
const httpRequest = require('../../utils/http_utils.js')
const dateutil = require('../../utils/date_util.js')
var regeneratorRuntime = require('../../utils/runtime.js');
var utils = require('../../utils/util')
const CryptoJS = require('../../utils/aes_util.js') //参数加密
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '搜索',
    PageSize: 10, //每页显示条数
    CurrentPage: 1, //当前页数
    EquipmentName: "", //任务名称（输入框的值）
    TaskType: 0, //任务类型
    historyList: [], //当天任务集合
    showIs: true, //图片显示
    noMore: false, //显示提示
    totalNum: 0, //数据条数
  },


  //监听输入框
  searchInput: function (e) {
    if (utils.isEmptyObject(e.detail.value)) {
      return
    }
    this.setData({
      EquipmentName: e.detail.value,
      PageSize: 10, //每页显示条数
      CurrentPage: 1, //当前页数
      historyList: [], //当天任务集合
      showIs: false, //图片显示
      noMore: false, //显示提示
    })
    this.initData();
  },

// cancelsearch
  cancelsearch: function (e) {
    this.setData({
      EquipmentName: "",
      historyList: [], //当天任务集合
      PageSize: 10, //每页显示条数
      CurrentPage: 1, //当前页数
    })
    this.initData();
  },

  // 跳转维保详情
  MaintainHistoryDetails: function (e) {
    let TaskGuid = e.currentTarget.dataset.taskguid;
    let EquipmentName = e.currentTarget.dataset.equipmentname;
    let RecordBeginTime = e.currentTarget.dataset.recordbegintime;
    let TaskCompleteTime = e.currentTarget.dataset.taskcompletetime;
    let EquipmentLocation = e.currentTarget.dataset.equipmentlocation;

    wx.navigateTo({
      url: '../MaintainHistoryDetails/MaintainHistoryDetails?TaskGuid=' + TaskGuid + "&EquipmentName=" + EquipmentName + "&RecordBeginTime=" + RecordBeginTime + "&TaskCompleteTime=" + TaskCompleteTime + "&EquipmentLocation=" + EquipmentLocation
    })

  },


  initData: async function () { //初始化数据
    var that = this;

    var Param = {
      PageSize: that.data.PageSize,
      CurrentPage: that.data.CurrentPage,
      EquipmentName: that.data.EquipmentName,
      TaskType: that.data.TaskType,
    }
    console.log("参数" + JSON.stringify(Param))

   


    let data = await utils.post(httpRequest.apiInfo.GetMaintenanceTaskHisMobileApi, Param)
    console.log(data)

    if (this.data.CurrentPage == 1 && utils.isEmptyObject(data.data.Data)) {
      this.setData({
        showIs: true
      })
    } else if (this.data.CurrentPage != 1 && utils.isEmptyObject(data.data.Data)) {
      that.setData({
        noMore: true
      })
    } else{
      let historyListnew = data.data.Data;
      let historyList = this.data.historyList;
      console.log(historyListnew)
      for (let item of historyListnew) {
        historyList.push(item)
      }

      this.setData({
        historyList: historyList,
        TotalNum: data.data.TotalNum,
        noMore: false,
      })
    }

   
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.initData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    console.log(that.data.TotalNum + "_______总条数")
    var pages = Math.round((that.data.TotalNum / 10) * 100) / 100
    if (that.data.CurrentPage > pages) {
      that.setData({
        noMore: true
      })
    } else {
      that.setData({
        noMore: false,
        CurrentPage: that.data.CurrentPage + 1
      })
      
      that.initData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})